package com.jr.daoimpl;

import com.jr.dao.WhiteListDao;
import com.jr.entity.Enterprise;
import com.jr.pojo.WeQuery;
import com.jr.pojo.WhiteEnterprise;
import com.jr.pojo.WhiteSupName;
import util.DBHelper;
import util.PageHelper;

import java.util.ArrayList;

public class WhiteListDaoImpl implements WhiteListDao {
DBHelper dbHelper = new DBHelper();
    @Override
    public int countTotalData(WeQuery weQuery,int enterprise_id) {
        StringBuilder sql = new StringBuilder("select count(w.wid) from enterprise e join supplier_white_list w on e.eid = w.supplier_id where w.enterprise_id=?");
        ArrayList list = new ArrayList();
        list.add(enterprise_id);
        if(!(weQuery.getEnamess().equals(""))){
            sql.append(" and e.enamess like ? ");
            list.add("%"+weQuery.getEnamess()+"%");
        }if(!(weQuery.getSupplier_level().equals(""))){
            sql.append(" and w.supplier_level=?");
            list.add(weQuery.getSupplier_level());
        }
        return dbHelper.queryCountData(sql.toString(),list.toArray());
    }

    @Override
    public ArrayList<WhiteEnterprise> selectIndexPageData(PageHelper pageHelper, WeQuery weQuery,int enterprise_id) {
        StringBuilder sql = new StringBuilder("select w.wid,e.enamess,e.ephone,e.email,w.supplier_level,w.update_date from enterprise e join supplier_white_list w on e.eid=w.supplier_id\n" +
                "where w.enterprise_id=?");
        ArrayList list = new ArrayList();
        list.add(enterprise_id);
        if(!(weQuery.getEnamess().equals(""))){
            sql.append(" and e.enamess like ? ");
            list.add("%"+weQuery.getEnamess()+"%");
        }if(!(weQuery.getSupplier_level().equals(""))){
            sql.append(" and w.supplier_level=?");
            list.add(weQuery.getSupplier_level());
        }
        sql.append(" limit ?,?");
        int index = (pageHelper.getIndexPage()-1)*pageHelper.getShowData();
        list.add(index);
        list.add(pageHelper.getShowData());
        System.out.println(sql.toString()+"....WhiteListDaoImpl.....");
        return dbHelper.query(sql.toString(),WhiteEnterprise.class,list.toArray());
    }


    @Override
    public ArrayList selectWhiteSupName(int eid) {
        String sql = "select e.enamess from enterprise e join supplier_white_list w on e.eid = w.supplier_id where w.enterprise_id=?";
        return dbHelper.query(sql, WhiteSupName.class,eid);
    }


    @Override
    public Enterprise selectCodeByName(String enamess) {
        String sql = "select * from enterprise where enamess=?";
        ArrayList list =  dbHelper.query(sql, Enterprise.class,enamess);
        return (Enterprise) list.get(0);
    }


    @Override
    public int addSup(int enterprise_id, int supplier_id, String supplier_level, String update_date) {
        String sql = "insert into supplier_white_list(enterprise_id,supplier_id,supplier_level,update_date) values(?,?,?,?)";
        Object[] objects = {enterprise_id,supplier_id,supplier_level,update_date};
        return dbHelper.update(sql,objects);
    }

    @Override
    public int delSup(int enterprise_id,int supplier_id) {
        String sql = "delete from supplier_white_list where enterprise_id=? and supplier_id=?";
        return dbHelper.update(sql,enterprise_id,supplier_id);
    }
}
